package com.web.kdl.service.alsert;

import com.web.kdl.dao.alert.AlertDao;
import com.web.kdl.daoV2.siteWheel.SiteWheelDao;
import com.web.kdl.entity.alert.Alert;
import com.web.kdl.entity.maDataReport.DataReport;
import com.web.kdl.entity.sitewheel.SiteWheel;
import com.web.kdl.pojo.form.AlertForm;
import com.web.kdl.util.TableNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author： shulibin
 * @Date： 2025/7/21 10:29
 * @Describe：
 */
@Service
public class AlertServiceImpl implements AlertService {
    @Resource
    private AlertDao alertDao;
    @Autowired
    SiteWheelDao siteWheelDao;
    @Override
    public List<Alert> getAllAlertData() {
//        ArrayList<Alert> AllAlert = new ArrayList<>();
//        List<SiteWheel> distinctSiteWheels= getSiteWheel();
//        for (SiteWheel siteWheel : distinctSiteWheels) {
//            String tableName = TableNameUtils.getAlertTableName(siteWheel.getSumSite(), siteWheel.getSubSite());
//            List<Alert> dataReports = alertDao.getAllAlertData(tableName);
//            AllAlert.addAll(dataReports);
//        }
//        return AllAlert;
        return null;
    }

    @Override
    public List<Map<String, Object>> getAlertData(AlertForm alertForm) {
//        ArrayList<Alert> AllAlert = new ArrayList<>();
//        List<SiteWheel> distinctSiteWheels= getSiteWheel();
//        for (SiteWheel siteWheel : distinctSiteWheels) {
//            String tableName = TableNameUtils.getAlertTableName(siteWheel.getSumSite(), siteWheel.getSubSite());
//            List<Alert> alerts = alertDao.selectAllHistoryEventByTime(tableName,alertForm.getTime());
//            AllAlert.addAll(alerts);
//        }
//        List<Map<String, Object>> method = method(AllAlert);
//        return method;
        return  null;
    }

    private List<Map<String, Object>> method(List<Alert> alerts) {
        List<Map<String, Object>> result = new ArrayList<>();

        // 一级分组：sum_site
        Map<String, List<Alert>> bySumSite = alerts.stream()
                .collect(Collectors.groupingBy(Alert::getSumSite));

        for (Map.Entry<String, List<Alert>> sumEntry : bySumSite.entrySet()) {
            String sumSite = sumEntry.getKey();
            List<Alert> sumSiteReports = sumEntry.getValue();

            Map<String, Object> sumSiteMap = new HashMap<>();
            sumSiteMap.put("name", sumSite);
            List<Map<String, Object>> subSites = new ArrayList<>();

            // 二级分组：sub_site
            Map<String, List<Alert>> bySubSite = sumSiteReports.stream()
                    .collect(Collectors.groupingBy(Alert::getSubSite));

            for (Map.Entry<String, List<Alert>> subEntry : bySubSite.entrySet()) {
                String subSite = subEntry.getKey();
                List<Alert> subSiteReports = subEntry.getValue();

                Map<String, Object> subSiteMap = new HashMap<>();
                subSiteMap.put("name", subSite);
                List<Map<String, Object>> dataNames = new ArrayList<>();

                // 三级分组：data_name（替换原先的 device_name） 具体名称 查看指定数据字段
                Map<String, List<Alert>> byDataName = subSiteReports.stream()
                        .collect(Collectors.groupingBy(Alert::getDeviceName));

                for (Map.Entry<String, List<Alert>> dataNameEntry : byDataName.entrySet()) {
                    String dataName = dataNameEntry.getKey();
                    List<Alert> dataList = dataNameEntry.getValue();

                    Map<String, Object> dataNameMap = new HashMap<>();
                    dataNameMap.put("name", dataName);
                    dataNameMap.put("data", dataList); // 最底层原始数据列表

                    dataNames.add(dataNameMap);
                }

                subSiteMap.put("children", dataNames);
                subSites.add(subSiteMap);
            }

            sumSiteMap.put("children", subSites);
            result.add(sumSiteMap);
        }
        return result;
    }
    public List<SiteWheel> getSiteWheel(){
//      获取表名
        List<SiteWheel> siteWheels = siteWheelDao.selectList(null);
        List<SiteWheel> distinctSiteWheels = siteWheels.stream()
                .distinct() // 去重
                .collect(Collectors.toList());
        return distinctSiteWheels;
    }
}
